package com.linksfield.topup.service.impl;

import com.alibaba.fastjson.JSON;
import com.linksfield.topup.config.dto.PageAndSort;
import com.linksfield.topup.config.dto.ResultDataDto;
import com.linksfield.topup.config.dto.StringOutputConstant;
import com.linksfield.topup.entity.common.SysOperate;
import com.linksfield.topup.entity.interfaces.accountmanage.create.request.RequestCreateAccount;
import com.linksfield.topup.entity.interfaces.accountmanage.operate.request.RequestAccountOperate;
import com.linksfield.topup.entity.interfaces.accountmanage.search.request.RequestAccountSearch;
import com.linksfield.topup.entity.interfaces.accountmanage.search.response.ResponseAccountSearch;
import com.linksfield.topup.entity.interfaces.login.request.RequestConfirmChangePwd;
import com.linksfield.topup.entity.interfaces.login.response.ResponseClickChangePwd;
import com.linksfield.topup.entity.interfaces.login.response.ResponseLogin;
import com.linksfield.topup.mapper.PartnerConfigMapper;
import com.linksfield.topup.mapper.SysOperateMapper;
import com.linksfield.topup.service.SysOperateService;
import com.linksfield.topup.utils.JwtObj;
import com.linksfield.topup.utils.JwtUtil;
import com.linksfield.topup.utils.MD5;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("operateService")
@Transactional(rollbackFor = Exception.class)

/**
 * 系统用户操作业务：逻辑处理实现类
 *
 * @author: lm
 * @date: 2019-11-21 15:40
 */

public class SysOperateServiceImpl implements SysOperateService {

    private static Logger logger = Logger.getLogger(SysOperateServiceImpl.class);

    @Resource
    SysOperateMapper operateMapper;

    @Autowired
    CommonUtilsServiceImpl utilsService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 验证账号和密码的有效性，操作登录
     *
     * @param account
     * @param password
     * @return
     */
    @Override
    public ResultDataDto checkLogin(String account, String password) {

        Integer c = operateMapper.queryAccount(account);
        if (c <= 0) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }

        SysOperate user = operateMapper.checkLogin(account, password);
        if (null == user) {
            //账号存在还不能登录肯定是密码有误
            logger.info("密码错误");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PASSWORD_ERROR);
        }

        //看看输入的账号和实际的账号是不是一致的，主要是中英文方面
        if (!user.getOperAccount().equals(account)) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }

        //用户被锁定
        if (StringOutputConstant.CODE_BLOCK.equals(user.getOperStatus())) {
            logger.info("用户被锁定了");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_BLOCK_ERROR);
        }
        //验证通过，现在开始操作登录 //有效期限是12小时
        JwtObj jwtObj = new JwtObj();
        jwtObj.setUserName(account);
        jwtObj.setUserPwd(password);
        jwtObj.setId(user.getOperId());
        jwtObj.setExpires(StringOutputConstant.CODE_EXPIRES);

        String token = JwtUtil.createJwt(jwtObj);
        //登录之前将token参数更新到数据库
        SysOperate o = new SysOperate();
        o.setOperAccount(account);
        o.setToken(token);
        o.setUpdate_time(s.format(new Date()));
        operateMapper.updateToken(o);

        //开始操作返回
        ResponseLogin response = new ResponseLogin();
        if (StringUtils.isNotBlank(user.getPartnerCode())) {
            response.setPartnerCode(user.getPartnerCode());
        } else {
            response.setPartnerCode("");
        }
        response.setAccount(account);
        response.setRolesId(user.getRolesId());
        response.setToken(token);
        logger.info("登录返回：" + JSON.toJSONString(response));
        return ResultDataDto.addOperationSuccess().setData(response);
    }


    /***
     * 验证token的有效性，操作退出登录
     * @param token
     * @return
     */
    @Override
    public ResultDataDto checkLogout(String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        Claims claims = JwtUtil.parseJWT(token);
        if (null == claims) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_INVALID);
        }
        if (StringUtils.isBlank((String) claims.get(StringOutputConstant.CODE_USERNAME)) || StringUtils.isBlank((String) claims.get(StringOutputConstant.CODE_USERPWD))) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_INVALID);
        }

        String account = (String) claims.get(StringOutputConstant.CODE_USERNAME);
        String password = (String) claims.get(StringOutputConstant.CODE_USERPWD);

        Integer c = operateMapper.queryAccount(account);
        if (c <= 0) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }

        SysOperate user = operateMapper.checkLogin(account, password);

        if (null == user) {
            logger.info("密码错误");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PASSWORD_ERROR);
        }

        //看看输入的账号和实际的账号是不是一致的，主要是中英文方面
        if (!user.getOperAccount().equals(account)) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }

        //用户被锁定
        if (StringOutputConstant.CODE_BLOCK.equals(user.getOperStatus())) {
            logger.info("用户被锁定");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_BLOCK_ERROR);
        }

        if (StringUtils.isBlank(user.getToken()) || !user.getToken().equals(token)) {
            // 一人登录之后紧接着另一人也登录，token信息已经更新，此时只能让其重新登录！
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_SAME_TIME_LOGIN_ERROR);
        }
        //退出登录之前将token置空
        SysOperate o = new SysOperate();
        o.setOperAccount(account);
        o.setToken("");
        o.setUpdate_time(s.format(new Date()));
        operateMapper.updateToken(o);
        logger.info("退出登录成功");
        //返回退出登录指令
        return ResultDataDto.addOperationSuccess();
    }

    /**
     * 验证token的有效性，点击打开"修改密码"框
     *
     * @param token
     * @return
     */

    @Override
    public ResultDataDto clickChangePwd(String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        Claims claims = JwtUtil.parseJWT(token);
        if (null == claims) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_INVALID);
        }
        if (StringUtils.isBlank((String) claims.get("userName")) || StringUtils.isBlank((String) claims.get("userPwd"))) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_INVALID);
        }
        String account = (String) claims.get("userName");
        String password = (String) claims.get("userPwd");

        Integer c = operateMapper.queryAccount(account);
        if (c <= 0) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }

        SysOperate user = operateMapper.checkLogin(account, password);

        if (null == user) {
            logger.info("密码错误");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PASSWORD_ERROR);
        }

        //看看输入的账号和实际的账号是不是一致的，主要是中英文方面
        if (!user.getOperAccount().equals(account)) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }

        //用户被锁定
        if (StringOutputConstant.CODE_BLOCK.equals(user.getOperStatus())) {
            logger.info("用户被锁定");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_BLOCK_ERROR);
        }
        //管理员账号和密码不能修改
        if (StringOutputConstant.CODE_ADMIN.equals(user.getRolesId())) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_OP_ADMIN_ERROR);
        }
        //token值对不上
        if (StringUtils.isBlank(user.getToken()) || !user.getToken().equals(token)) {
            // 一人登录之后紧接着另一人也登录，token信息已经更新，此时只能让其重新登录！
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_SAME_TIME_LOGIN_ERROR);
        }

        //确认安全，可以打开更改密码框：并且给前端一个newPassword参数作为下一个“确认修改”上传的参数
        ResponseClickChangePwd response = new ResponseClickChangePwd();
        response.setPartnerCode(user.getPartnerCode());
        response.setPartnerName(user.getPartnerName());
        response.setRolesId(user.getRolesId());
        response.setAccount(user.getOperAccount());
        response.setNewPassword("");
        logger.info("修改密码成功");
        return ResultDataDto.addOperationSuccess().setData(response);


    }

    /***
     * 点击修改密码页面上的"确认修改"按钮
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto confirmChangePwd(RequestConfirmChangePwd request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        Claims claims = JwtUtil.parseJWT(token);
        if (null == claims) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_INVALID);
        }
        if (StringUtils.isBlank((String) claims.get(StringOutputConstant.CODE_USERNAME)) || StringUtils.isBlank((String) claims.get(StringOutputConstant.CODE_USERPWD))) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_INVALID);
        }
        String account = (String) claims.get(StringOutputConstant.CODE_USERNAME);
        String password = (String) claims.get(StringOutputConstant.CODE_USERPWD);

        Integer c = operateMapper.queryAccount(account);
        if (c <= 0) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }
        SysOperate user = operateMapper.checkLogin(account, password);
        if (null == user) {
            logger.info("密码错误");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PASSWORD_ERROR);
        }

        //看看输入的账号和实际的账号是不是一致的，主要是中英文方面
        if (!user.getOperAccount().equals(account)) {
            logger.info("账号不存在");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_ERROR);
        }

        //用户被锁定
        if (StringOutputConstant.CODE_BLOCK.equals(user.getOperStatus())) {
            logger.info("用户被锁定");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ACCOUNT_BLOCK_ERROR);
        }
        //管理员账号和密码不能修改
        if (StringOutputConstant.CODE_ADMIN.equals(user.getRolesId())) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_OP_ADMIN_ERROR);
        }
        //token值对不上
        if (StringUtils.isBlank(user.getToken()) || !user.getToken().equals(token)) {
            // 一人登录之后紧接着另一人也登录，token信息已经更新，此时只能让其重新登录！
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_SAME_TIME_LOGIN_ERROR);
        }
        if (StringUtils.isBlank(request.getRolesId()) || StringUtils.isBlank(request.getAccount()) || StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getNewPassword()) || StringUtils.isBlank(request.getOldPassword()) || StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }
        //验证原密码是不是正确，原密码错误不允许修改密码
        Integer count = operateMapper.queryOldPwd(request.getAccount(), MD5.calcMD5(request.getOldPassword()));
        if (count <= 0) {
            logger.info("原密码输入有误，不允许修改");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_OLD_PASSWORD_ERROR);
        }

        //判断修改的新密码是不是在6-16位范围内
        if (6 > lengthOfUTF8(request.getNewPassword()) || 16 < lengthOfUTF8(request.getNewPassword())) {
            logger.info("新密码小于6位或者大于16位");
            return ResultDataDto.addOperationFailure("请输入6-16位数字、字母或组合密码");
        }

        //验证输入的新密码和原密码是否一致
        if (user.getOperPassword().equalsIgnoreCase(MD5.calcMD5(request.getNewPassword()))) {
            // 上报的新密码与旧密码相同
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_EDIT_PASSWORD_TIPS);
        }

        //确认修改密码
        SysOperate o = new SysOperate();
        o.setPartnerCode(request.getPartnerCode());
        o.setOperAccount(request.getAccount());
        o.setOperPassword(MD5.calcMD5(request.getNewPassword()));
        o.setToken("");
        Integer co = operateMapper.changePwd(o);
        if (co > 0) {
            logger.info("修改密码成功");
            return ResultDataDto.addOperationSuccess(StringOutputConstant.CODE_EDIT_PASSWORD_SUCCESS);
        }
        logger.info("修改密码失败");
        return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_EDIT_PASSWORD_FAILD);


    }

    /****
     *系统用户管理：搜索功能
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto accountSearch(RequestAccountSearch request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token,以及权限
        String results = utilsService.checkAdmin(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) || null == request.getCondition() || null == request.getRp() || null == request.getCurrentPage()) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //1,先去把分页信息拿回来
        long rowCount = operateMapper.searchPageCount(request);
        //如果前端未指定查询页码和显示多少行
        Integer rp;
        if (null != request.getRp()) {
            rp = request.getRp();
        } else {
            rp = 10;
        }

        Integer currentPage;
        if (null != request.getCurrentPage()) {
            currentPage = request.getCurrentPage();
        } else {
            currentPage = 1;
        }

        //计算出来的分页的数据
        PageAndSort pageHelper = new PageAndSort();
        pageHelper.setPage(currentPage);
        pageHelper.setRp(rp);
        pageHelper.setRowCount(rowCount);

        //2,拿着分页信息去数据库查询具体的用户数据列表记录
        List<SysOperate> accountList = operateMapper.accountSearch(request, pageHelper);

        List<ResponseAccountSearch> responseList = new ArrayList<ResponseAccountSearch>();
        ResponseAccountSearch re = null;

        for (SysOperate user : accountList) {

            re = new ResponseAccountSearch();
            re.setAccount(user.getOperAccount());
            re.setPartnerCode(user.getPartnerCode());
            re.setPartnerName(user.getPartnerName());
            re.setOperStatus(user.getOperStatus());
            try {
                re.setCreateDate(sdf.format(s.parse(s.format(user.getCreate_time()))));
                if (StringUtils.isNotBlank(user.getUpdate_time())) {
                    re.setLoginDate(sdf.format(s.parse(user.getUpdate_time())));
                } else {
                    re.setLoginDate("-");
                }
            } catch (Exception e) {
                logger.info("时间类型转换发生异常！");
            }
            responseList.add(re);
        }
        logger.info("系统用户管理：搜索:" + JSON.toJSONString(responseList));
        //成功：给前端统一返回
        return ResultDataDto.addOperationSuccess().setData(responseList).setPageAndSort(pageHelper);
    }

    /***
     * 系统用户管理：操作行为：1-重置密码 ；2-封禁账号；3-启用账号
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto accountOperate(RequestAccountOperate request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token,以及权限
        String results = utilsService.checkAdmin(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }

        //管理员账号不允许重置密码，不允许封禁账号，先在这边做判断，因为管理员请求时参数不齐全
        boolean b1 = StringUtils.isNotBlank(request.getAccount()) && StringUtils.isNotBlank(request.getOperateStatus()) &&
                ("test".equals(request.getAccount()) || "admin".equals(request.getAccount()) || "dev".equals(request.getAccount())) &&
                ("1".equals(request.getOperateStatus()) || "2".equals(request.getOperateStatus()));
        if (b1) {
            logger.info("管理员账号不允许重置密码，不允许封禁账号");
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_RIGHT_ERROR);
        }

        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) ||
                StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getOperateStatus()) || StringUtils.isBlank(request.getAccount())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //操作码有误,指令有误
        if (!"1".equals(request.getOperateStatus()) && !"2".equals(request.getOperateStatus()) && !"3".equals(request.getOperateStatus())) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ORDER_ERROR);
        }

        //先去查询一下目前用户是什么状态
        String status = operateMapper.checkOperStatus(request.getPartnerCode(), request.getAccount());

        //1，重置密码
        if ("1".equals(request.getOperateStatus())) {
            SysOperate o = new SysOperate();
            o.setToken("");
            o.setOperPassword(MD5.calcMD5("888888"));
            o.setPartnerCode(request.getPartnerCode());
            o.setOperAccount(request.getAccount());
            Integer count = operateMapper.changePwd(o);

            if (count > 0) {
                return ResultDataDto.addOperationSuccess("密码重置成功！");
            }
            return ResultDataDto.addOperationFailure("密码重置失败，请稍后重试！");
        }
        //2,封禁账号
        if ("2".equals(request.getOperateStatus())) {

            if (!StringOutputConstant.CODE_NORMAL.equals(status)) {
                return ResultDataDto.addOperationFailure("账号封禁失败，账号不符合封禁规则");
            }
            SysOperate o = new SysOperate();
            o.setToken("");
            o.setOperStatus(StringOutputConstant.CODE_BLOCK);
            o.setPartnerCode(request.getPartnerCode());
            o.setOperAccount(request.getAccount());
            Integer count = operateMapper.operateOperStatus(o);

            if (count > 0) {
                return ResultDataDto.addOperationSuccess("账号封禁成功！");
            }
            return ResultDataDto.addOperationFailure("账号封禁失败，请稍后重试！");


        }
        //3,启用账号
        if ("3".equals(request.getOperateStatus())) {

            if (!StringOutputConstant.CODE_BLOCK.equals(status)) {
                return ResultDataDto.addOperationFailure("账号启用失败，账号不符合启用规则！");
            }
            SysOperate o = new SysOperate();
            o.setToken("");
            o.setOperStatus(StringOutputConstant.CODE_NORMAL);
            o.setPartnerCode(request.getPartnerCode());
            o.setOperAccount(request.getAccount());
            Integer count = operateMapper.operateOperStatus(o);

            if (count > 0) {
                return ResultDataDto.addOperationSuccess("账号启用成功！");
            }
            return ResultDataDto.addOperationFailure("账号启用失败，请稍后重试！");

        }

        return null;
    }

    /***
     * 系统用户管理：创建用户
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto createAccount(RequestCreateAccount request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token,以及权限
        String results = utilsService.checkAdmin(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }

        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) || StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getAccount())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        Integer count = operateMapper.checkPartner(request.getPartnerCode());

        if (count >= 1) {
            return ResultDataDto.addOperationFailure("企业已有系统账号！");
        }

        String partnerName = operateMapper.queryNameByCode(request.getPartnerCode());
        SysOperate o = new SysOperate();
        o.setRolesId(StringOutputConstant.CODE_PARTNER);
        o.setOperAccount(request.getAccount());
        o.setOperPassword(MD5.calcMD5("888888"));
        o.setOperDesc("普通用户");
        o.setOperStatus(StringOutputConstant.CODE_NORMAL);
        o.setPartnerName(partnerName);
        o.setPartnerCode(request.getPartnerCode());
        Integer count1 = operateMapper.createAccount(o);

        if (count1 > 0) {
            return ResultDataDto.addOperationSuccess("创建账户成功！");
        }
        return ResultDataDto.addOperationFailure("创建账户失败！");

    }

    /**
     * 判断字符串的长度的方法
     *
     * @param value
     * @return
     */
    public int lengthOfUTF8(String value) {
        int valueLength = 0;
        if (value == null) {
            return valueLength;
        }
        String chinese = "[\u0391-\uFFE5]";
        /* 获取字段值的长度，如果含中文字符，则每个中文字符长度为3，否则为1 */
        for (int i = 0; i < value.length(); i++) {
            /* 获取一个字符 */
            String temp = value.substring(i, i + 1);
            /* 判断是否为中文字符 */
            if (temp.matches(chinese)) {
                /* 中文字符长度为3 */
                valueLength += 3;
            } else {
                /* 其他字符长度为1 */
                valueLength += 1;
            }
        }
        return valueLength;
    }
}